from docutils.core import publish_parts
from docutils.parsers.rst import directives, roles, Directive
from docutils.parsers import rst
from docutils import nodes, utils

from custom_directives import Pygments, wiki_reference_role


class RSTRenderer():
    """The RSTRenderer uses docutils to render reStructuredText content."""

    def __init__(self):
        super().__init__()
        directives.register_directive('sourcecode', Pygments)
        roles.register_local_role("wikiref", wiki_reference_role)

    def render_string(self, astring):
        astring = '.. sectnum::\n\n' + astring
        # astring = '.. contents:: Table of Contents\n\n' + astring

        return publish_parts(astring, writer_name='html')['html_body']
